package tiandang.core.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tiandang.core.dao.ProductMapper;
import tiandang.core.dao.ProductStockMapper;
import tiandang.core.error.BusinessException;
import tiandang.core.error.EmBusinessError;
import tiandang.core.form.ProductForm;
import tiandang.core.model.OrderProduct;
import tiandang.core.model.Product;
import tiandang.core.model.ProductStock;
import tiandang.core.service.ProductService;
import tiandang.core.validator.ValidationResult;
import tiandang.core.validator.ValidatorImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService {

  @Autowired
  private ValidatorImpl validator;

  @Autowired
  private ProductMapper productMapper;

  @Autowired
  private ProductStockMapper productStockMapper;

  @Override
  @Transactional
  public void createProduct(ProductForm productForm) throws BusinessException {

    //校验入参
    ValidationResult result = validator.validate(productForm);
    if (result.isHasErrors()) {
      throw new BusinessException(EmBusinessError.PARAMMETER_VALIDATION_ERROR, result.getErrMsg());
    }

    Product product = new Product();
    product.setName(productForm.getName());
    product.setImgName(productForm.getImgName());
    product.setImgUrl(productForm.getImgUrl());
    product.setOriginPrice(productForm.getOriginPrice());
    product.setPresentPrice(productForm.getPresentPrice());
    product.setProportion(productForm.getProportion());
    product.setDescription(productForm.getDescription());
    product.setIsLimit(productForm.getIsLimit());
    if (productForm.getLimitBuy() != null) {
      product.setLimitBuy(productForm.getLimitBuy());
    }

    if (productForm.getMock() != null) {
      product.setMock(productForm.getMock());
    }
    productMapper.insertSelective(product);

    ProductStock productStock = new ProductStock();
    productStock.setProductId(product.getId());
    productStock.setStock(productForm.getStock());

    productStockMapper.insertSelective(productStock);
  }

  @Override
  @Transactional
  public void modifyProduct(ProductForm productForm) throws BusinessException {

    //校验入参
    ValidationResult result = validator.validate(productForm);
    if (result.isHasErrors()) {
      throw new BusinessException(EmBusinessError.PARAMMETER_VALIDATION_ERROR, result.getErrMsg());
    }

    Product product = new Product();
    product.setId(productForm.getId());
    product.setName(productForm.getName());
    product.setImgName(productForm.getImgName());
    product.setImgUrl(productForm.getImgUrl());
    product.setOriginPrice(productForm.getOriginPrice());
    product.setPresentPrice(productForm.getPresentPrice());
    product.setProportion(productForm.getProportion());
    product.setDescription(productForm.getDescription());
    product.setIsLimit(productForm.getIsLimit());
    if (productForm.getLimitBuy() != null) {
      product.setLimitBuy(productForm.getLimitBuy());
    }

    if (productForm.getMock() != null) {
      product.setMock(productForm.getMock());
    }

    productMapper.updateByPrimaryKeySelective(product);

    ProductStock productStock = new ProductStock();
    productStock.setProductId(product.getId());
    productStock.setStock(productForm.getStock());

    productStockMapper.updateByProductId(productStock);

  }

  @Override
  @Transactional
  public void delete(Integer id) {
    productMapper.deleteByPrimaryKey(id);
    productStockMapper.deleteByByProductId(id);
  }

  @Override
  @Transactional
  public void decreaseStock(List<OrderProduct> orderProductList) throws BusinessException {
    for (OrderProduct orderProduct : orderProductList) {
      productStockMapper.decreaseStock(orderProduct.getProductId(), orderProduct.getProductNumber());
    }
  }

  @Override
  @Transactional
  public void increaseStock(List<OrderProduct> orderProductList) throws BusinessException {
    for (OrderProduct orderProduct : orderProductList) {
      productStockMapper.increaseStock(orderProduct.getProductId(), orderProduct.getProductNumber());
    }
  }

  @Override
  @Transactional
  public void increaseSales(List<OrderProduct> orderProductList) throws BusinessException {
    for (OrderProduct orderProduct : orderProductList) {
      productMapper.increaseSales(orderProduct.getProductId(), orderProduct.getProductNumber());

    }
  }

  @Override
  public void decreaseSales(List<OrderProduct> orderProductList) throws BusinessException {
    for (OrderProduct orderProduct : orderProductList) {
      productMapper.decreaseSales(orderProduct.getProductId(), orderProduct.getProductNumber());
    }
  }

  @Override
  public List<Product> getListByProductIdList(List<Integer> productIdList) {
    return productMapper.getListByIdList(productIdList);
  }

  @Override
  public List<Product> getAllNormal() {
    return productMapper.getAllNormal();
  }

  @Override
  public Object getList(String name, Integer currentPage, Integer limit) {
    Map<String, Object> map = new HashMap<>();
    int offset = (currentPage - 1) * limit;
    List<Product> productList = productMapper.getListByParams(name, limit, offset);
    int totalCount = productMapper.countByParams(name);
    map.put("productList", productList);
    map.put("totalCount", totalCount);
    return map;
  }

  @Override
  public Product getProductById(Integer id) {
    return productMapper.selectByPrimaryKey(id);
  }

  @Override
  public void setDownline(Integer id, Integer downline) {
    productMapper.setDownline(id, downline);
  }
}
